using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;

namespace ERP.View
{
    public partial class viewListNhomnguoidung : DevExpress.XtraEditors.XtraForm
    {
        public viewListNhomnguoidung()
        {
            InitializeComponent();
        }

        public cs_hmr_nhom_nguoi_dungInfo nhom = new cs_hmr_nhom_nguoi_dungInfo();
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
        public cs_hmr_nhanvienInfo nv = new cs_hmr_nhanvienInfo();
        private int flag = 0;
        private int count = 0;

        private void viewListNhomnguoidung_Load(object sender, EventArgs e)
        {
            this.Load_Data();
            this.CheckUser();
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnMoi.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                    this.btnXoa.Enabled = false;
                }
                if (userright.updatedata == 0)
                {
                    this.btnSua.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }

        private void Load_Data()
        {
            this.userright.groupid = this.user.groupid;
            //this.userright.
            DataSet ds = new DataSet("ds");
            ds = this.nhom.Get_List();
            this.count = ds.Tables[0].Rows.Count;
            this.grcDanhsach.DataSource = ds.Tables[0];
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

        }

        private void btnMoi_Click(object sender, EventArgs e)
        {
            this.flag = 1;
            this.txtTennhom.Text = "";
            this.txtGhichu.Text = "";
            this.btnLuu.Enabled = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle < this.count)
            {
                this.flag = 2;
                this.nhom.id_nhomnguoidung = int.Parse(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim());
                this.txtTennhom.Text = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcTennhom).ToString().Trim();
                this.txtGhichu.Text = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcGhichu).ToString().Trim();
                this.btnLuu.Enabled = true;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle < this.count)
            {
                this.flag = 3;
                this.nhom.id_nhomnguoidung = int.Parse(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim());
                this.nhom.ExcuteProcedure(this.flag);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            this.nhom.tennhom = this.txtTennhom.Text.Trim();
            this.nhom.mota = this.txtGhichu.Text.Trim();
            if (this.nhom.tennhom == "")
            {
            }
            else
            {
                this.nhom.ExcuteProcedure(this.flag);
                this.btnLuu.Enabled = false;
                this.Load_Data();
            }
        }
    }
}